using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;


namespace CapstoneProject
{
    public class OFXHome
    {
        public List<string> Search(string bank)
        {
            List<string> results = new List<string>();
            WebRequest request = WebRequest.Create("http://www.ofxhome.com/api.php?search=" + bank);
            request.Credentials = CredentialCache.DefaultCredentials;
            var stream = request.GetResponse().GetResponseStream();

            var reader = new StreamReader(stream);
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains(bank))
                {
                    results.Add(line);
                }
            }
            return results;
        }
        public List<string> AllBanks()
        {
            List<string> results = new List<string>();
            WebRequest request = WebRequest.Create("http://www.ofxhome.com/api.php?all=yes");
            request.Credentials = CredentialCache.DefaultCredentials;
            var stream = request.GetResponse().GetResponseStream();

            var reader = new StreamReader(stream);
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains("institutionid") && !line.ToLower().Contains("credit"))
                {
                    using (XmlReader xmlreader = XmlReader.Create(new StringReader(line)))
                    {
                        xmlreader.ReadToFollowing("institutionid");
                        xmlreader.MoveToFirstAttribute();
                        results.Add(xmlreader.Value);
                    }
                }
            }
            return results;
        }
    }
}